//
//  UIApplication+WMAppExtension.m
//  WaiMai
//
//  Created by Angela on 2017/8/24.
//  Copyright © 2017年 Angela. All rights reserved.
//

#import "UIApplication+WMAppExtension.h"
#import <objc/runtime.h>
NSNotificationName const SXSApplicationRootControllerChangedNotification = @"SXSApplicationRootControllerChangedNotification";
NS_INLINE UIViewController *currentViewControllerFrom(UIViewController *rootViewController) {
    // If the incoming controller is a navigation controller, return the last.
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *) rootViewController;
        return currentViewControllerFrom(navigationController.viewControllers.lastObject);
    }
    
    // If the incoming controller is a tabBar controller, return the selected one.
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return currentViewControllerFrom(tabBarController.selectedViewController);
    }
    
    // If the incoming controller has modal, get the presented controller.
    if (rootViewController.presentedViewController) {
        return currentViewControllerFrom(rootViewController.presentedViewController);
    }
    
    return rootViewController;
}

@implementation UIApplication (WMAppExtension)
+ (BOOL)isFirstLaunch {
    NSNumber *firstLaunch = objc_getAssociatedObject(self, _cmd);
    if (!firstLaunch) {
        /// 读取沙盒数据
        NSString *kAppVersion = @"appVersion";
        NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        NSString *version = [userDefault objectForKey:kAppVersion];
        BOOL isFirstLaunch = (NSOrderedDescending == [appVersion compare:version]);
        if (isFirstLaunch) {
            [userDefault setObject:appVersion forKey:kAppVersion];
        }
        firstLaunch = @(isFirstLaunch);
        objc_setAssociatedObject(self, _cmd, firstLaunch, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return [firstLaunch boolValue];
}

+ (WMBaseNavViewController *)rootViewController {
    return (WMBaseNavViewController *)([[[[self sharedApplication] delegate] window] rootViewController]);
}

+ (UIViewController *)currentViewController {
    return currentViewControllerFrom([self rootViewController]);
}

+ (WMBaseTabBarViewController *)tabBarController {
    WMBaseNavViewController *nav = [self rootViewController];
    __block WMBaseTabBarViewController *tab;
    if (nav) {
        [nav.viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[WMBaseTabBarViewController class]]) {
                tab = obj;
                *stop = YES;
            }
        }];
    }
    return tab;
}

+ (UIWindow *)delegateWindow {
    return [UIApplication sharedApplication].delegate.window;
}

@end
